package me.thinkjet.controller;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.ehcache.CacheInterceptor;
import com.jfinal.plugin.ehcache.CacheName;
import me.thinkjet.auth.AuthManager;
import me.thinkjet.model.Firm;
import me.thinkjet.model.News;

import java.util.List;

@ControllerBind(controllerKey = "/firm", viewPath = "firm")
public class FirmController extends Controller {
	@Before(CacheInterceptor.class)
	@CacheName("firm")
	public void index() {
		this.setAttr(
				"news",
				News.dao.paginateByCache("news",
						"index_" + this.getParaToInt(1, 1),
						this.getParaToInt(1, 1), 10, "select *", "from news"));
	}

	public void my() {
		if (AuthManager.getSessionUser(this) == null) {
			this.setAttr("error", "请先登录");
		} else {
			List<Firm> firms = Firm.dao.find("select id,name from firm where creator=?",
					AuthManager.getSessionUser(this).getUser().getLong("id"));
			if (firms.size() == 0) {
				this.setAttr("error", "没有公司");
			} else {
				this.setAttr("firms", firms);
			}

		}
	}
}
